package cn.tedu;

import cn.utiles.JDBCUtiles;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

//注解
@WebServlet(value = "/servlet/RegistServlet")
public class RegistServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");


        //请求到位了，获取表单信息
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String password2 = request.getParameter("password2");
        String nickname = request.getParameter("nickname");
        String email = request.getParameter("email");

        //校验
        //用户名校验
        if (username == null || "".equals(username)) {
            request.setAttribute("msg","用户名不能为空！");
            request.getRequestDispatcher(request.getContextPath()+"/regist.jsp")
                    .forward(request,response);
            return;
        }
        if (password == null || "".equals(password)) {
            request.setAttribute("msg","密码不能为空！");
            request.getRequestDispatcher(request.getContextPath()+"/regist.jsp")
                    .forward(request,response);
            return;
        }
        if (password2 == null || "".equals(password2)) {
            request.setAttribute("msg","确认密码不能为空！");
            request.getRequestDispatcher(request.getContextPath()+"/regist.jsp")
                    .forward(request,response);
            return;
        }
        if (nickname == null || "".equals(nickname)) {
            request.setAttribute("msg","昵称不能为空！");
            request.getRequestDispatcher(request.getContextPath()+"/regist.jsp")
                    .forward(request,response);
            return;
        }
        if (email == null || "".equals(email)) {
            request.setAttribute("msg","邮箱不能为空！");
            request.getRequestDispatcher(request.getContextPath()+"/regist.jsp")
                    .forward(request,response);
            return;
        }
        //密码是否一致校验
        if (!password.equals(password2)) {
            request.setAttribute("msg","密码不一致！");
            request.getRequestDispatcher(request.getContextPath()+"/regist.jsp")
                    .forward(request,response);
            return;
        }
        //邮箱校验
        if (!email.matches("^\\w+@\\w+(\\.\\w+)+$")) {
            request.setAttribute("msg","邮箱格式错误！");
            request.getRequestDispatcher(request.getContextPath()+"/regist.jsp")
                    .forward(request,response);
            return;
        }
        Connection conn = JDBCUtiles.getConn();
        //检查用户是否存在
        try {
            PreparedStatement ps = conn.prepareStatement(
                    "select *from user where username=?;");
            ps.setString(1,username);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                request.setAttribute("msg","用户已存在");
                request.getRequestDispatcher("/regist.jsp")
                        .forward(request,response);
                return;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //写入数据库

        try {
            PreparedStatement ps = conn.prepareStatement(
                    "insert into user values (null,?,?,?,?);");
            ps.setString(1,username);
            ps.setString(2,password);
            ps.setString(3,nickname);
            ps.setString(4,email);
            int i = ps.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //瞬间跳转到主页
//        response.sendRedirect(request.getContextPath()+"/index.jsp");

        response.getWriter().write("3秒后跳转！");

        response.setHeader("refresh",
                "3;url="+request.getContextPath()+"/index.jsp");


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
